package com.zx.article.article.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zx.article.article.model.ArticleModel;
import com.zx.article.article.service.IArticleService;
import com.zx.article.article.service.impl.ArticleServiceImpl;
import com.zx.article.util.FmtRequest;

@WebServlet("/ArticleServlet")
public class ArticleServlet extends HttpServlet {

	private static final long serialVersionUID = -6746298930072659166L;
	private FmtRequest fmtRequest = new FmtRequest();
	private IArticleService articleService = new ArticleServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object result = "未实现方法";
		String action = request.getParameter("action");
		switch (action) {
		case "add":
			result = add(request);
			break;
		case "get":
			result = get(request);
			break;
		case "del":
			result = del(request);
			break;
		case "getModel":
			result = getModel(request);
			break;
		case "upd":
			result = upd(request);
			break;
		case "getCase":
			result = getCase(request);
			break;
		case "addNum":
			result = addNum(request);
			break;
		default:
			break;
		}
		fmtRequest.write(response.getWriter(), result);
	}

	private String addNum(HttpServletRequest request) {
		String parameter = request.getParameter("wordNum");
		BigDecimal wordNum = null;
		try {
			wordNum = new BigDecimal(Integer.valueOf(parameter));
		} catch (NumberFormatException e) {
			return "number";
		}
		ArticleModel articleModel = new ArticleModel();
		articleModel.setId(Integer.valueOf(request.getParameter("id")));
		articleModel.setWordNum(wordNum);
		return articleService.upd(articleModel);
	}

	private Map<String, Object> getCase(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
//		1-count
		ArticleModel parseModel = fmtRequest.parseModel(request, ArticleModel.class);
		parseModel.setType(request.getParameter("type").isEmpty() ? null : request.getParameter("type"));
		int count = articleService.getCaseCount(parseModel);
		map.put("count", count);
//		2-Data
		parseModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		parseModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		parseModel.setPageOn(true);
		List<ArticleModel> list = articleService.getCaseList(parseModel);
		map.put("list", list);
		return map;
	}

	private String upd(HttpServletRequest request) {
		String title = request.getParameter("title");
		String key = request.getParameter("key");
		Integer typeId = Integer.valueOf(request.getParameter("typeId"));
		String WritterAccount = request.getParameter("WritterAccount");
		String url = request.getParameter("url");
		String message = request.getParameter("message");
		String time = request.getParameter("time");
		String fileName = request.getParameter("fileName");
		ArticleModel articleModel = new ArticleModel(title, key, typeId, WritterAccount, fileName, url, message, time);
		articleModel.setId(Integer.valueOf(request.getParameter("id")));
		return articleService.upd(articleModel);
	}

	private ArticleModel getModel(HttpServletRequest request) {
		Integer id = Integer.valueOf(request.getParameter("id"));
		return articleService.getModel(id);
	}

	private String del(HttpServletRequest request) {
		Integer id = Integer.valueOf(request.getParameter("id"));
		return articleService.del(id);
	}

	private Map<String, Object> get(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
//		1-count
		String title = request.getParameter("title");
		String key = request.getParameter("key");
		String author = request.getParameter("author");
		Integer typeId = request.getParameter("typeId").isEmpty() ? null
				: Integer.valueOf(request.getParameter("typeId"));
		ArticleModel parseModel = new ArticleModel(title, key, typeId, author);
		parseModel.setResult(request.getParameter("result").isEmpty() ? null : request.getParameter("result"));
		int count = articleService.getCount(parseModel);
		map.put("count", count);
//		2-Data
		parseModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		parseModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		parseModel.setPageOn(true);
		List<ArticleModel> list = articleService.getList(parseModel);
		map.put("list", list);
		return map;
	}

	private String add(HttpServletRequest request) {
		String title = request.getParameter("title");
		String key = request.getParameter("key");
		Integer typeId = Integer.valueOf(request.getParameter("typeId"));
		String WritterAccount = request.getParameter("WritterAccount");
		String url = request.getParameter("url");
		String message = request.getParameter("message");
		String time = request.getParameter("time");
		String fileName = request.getParameter("fileName");
		ArticleModel articleModel = new ArticleModel(title, key, typeId, WritterAccount, fileName, url, message, time);
		return articleService.add(articleModel);
	}

}
